<template>
  <div id="app" class="container">
    <router-view />
  </div>
</template>

<script>
import { termite } from "@/api/index";
import { markTokenReady } from "@/utils/request";
export default {
  name: "App",
  mounted() {
    // if (process.env.NODE_ENV === "development") {
    //   // 开发环境直接初始化，不依赖 message
    //   this.init();
    // } else {
    //   // 仅生产环境监听，且只触发一次
    //   window.addEventListener("message", this.handleMessage);
    // }
  },
  beforeDestroy() {
    // 组件销毁前清除轮询
    // this.stopPolling();
    window.removeEventListener("message", this.handleMessage);
  },
  methods: {
    init() {
      // 开发环境
      const data = {
        data: "haYSV1vDBR2ABKG5pzDlCSu5OQX1K3daR9UhlcB0wj8MplqSwvkhjTUtI+YE4gcMpj0l/5QB5LSpPOovEeQRwGc65YV0lHaPj+4hk9NqqmD2WHJiyuFId4n6fGI7NBkopgVByBh0z4MA9giM8/+c0jyf5Y0AtFEGoJN9GWva+Ie54k4EciXH3tOtvC7vppSga/IFHlkcnctUPVjjpQCwCdz0Vla+KShKfVe4kAT/QqzlHV0OBs3PGxGniyv5P4in1l8FpUlqo5vVF02rKoU6yti1nQiEROO/WicqaYUd6PNj6dVG31k23+kD4PpiPx+3+kNr/SQ7Ja/DCI/w4D6IUOj07z4Y5gkuJnilt5z/I3DyEltWnGPdYQv4NMCwjL3RYjJr4JXQc9SiLRZ7R5/w39tmanedLJ4U0xItMhm0U6EKHdChZ2YOUjPj3nnlbk5jDSbmGYoqixL+pDleexwob/ESzjIp2TdqzUMZZQsioXgO+sjhR1ZpieLK3PByN28kIAszwtaV2OkMKVdr+ytbh3Mqkv1eXT7Ss4Wxb5V4vfAxDyhYlfP+I8qSP+u6JQLpb1jiyVYMklT68aha9OiuTRTi7+PNu7B45xVTQB9Ax2eQgSfDNYhyWUY1ENJyUp0SRN4dRYLz2wS9anGGoLAwZhznlg4awfNmvsEB09ND7vMutDcw0nJ3NZZz5WW5vdvUfiqCMAcP6wuJFLIk7IeTsnnq+AJyNIdfkvLG1NY9MVF8gngsAV/tGatno9dA3OEWTDqueOhM8uHqc54URwlHqR+lo+p1giBEUMdTi5lM0q19W9sO3Vcy9ShWU6TYJdn42oGDq1jdqmK+CFs/FMbQZl2etUmkwE1oWAFT950tPxkp7uqOvDgyECLsTmnggtYNETCJ2keI6KMJIP8JLW4YVMDZL8rwWUczHMAuXfFf5flhhzmn00fkYoNMxAx+rw1v",
        secret:
          "ITJMvRWK4DuvutuZobFs6CjS4IgMZ4R7/iUV6IvzEkmuvjVvrwf3ZajYGrHyVqG/R8B78PfdeqLT+cTmPA7RSX6ztUMjrlamPvnXNN2cB+R8CTQjwWba90s7+Wy1RMqC1DDyz+FjrTEgleIDLLPx0IYLvrFgyJ41TCtrbL/M1nLLHTNlg43vP4ZAoBacmEJuCRmuwSIoiexcgpuud4zefmav5qM2BRnzK0xVY5W6uDxqYza0CcPL5/Uo0unj95ieqCMz5gnwRizlVYdjfj9RS39VkXNTITzpRKSpgaN7G3BCeXWEXeOaR3661NrFpgTfBiPVE36HoMRrJOMhrKYHEQ==",
        sysCode: "byznjc",
      };
      // 新的接口
      termite.getTokenNew(data).then((res) => {
        if (res.errorCode === "000000") {
          this.$store.commit("user/SET_USER_INFO", res.data);
          // 系统中用的token
          this.$store.commit("user/SET_TOKEN", res.data.token);
          // 通知请求层 token 已就绪
          markTokenReady();
        }
      });
    },
    handleMessage(event) {
      // const data = JSON.parse(decodeURIComponent(event.data));
      console.log(event.data, "接收消息");
      // 通知请求层 token 已就绪
      if (!event.data) return;
      // if (process.env.NODE_ENV === "development") {
      if (event.data === "webpackOk") return;
      if (
        event.data &&
        typeof event.data === "object" &&
        (event.data.type === "webpackOk" ||
          event.data.key === "__VUE_DEVTOOLS_VUE_DETECTED_EVENT__")
      )
        return;

      let decodeData = decodeURIComponent(event.data);
      console.log("解析后的数据decodeData:", data);
      const data = JSON.parse(decodeData);
      console.log("解析后的数据data:", data);
      try {
        termite.getTokenNew(data).then((res) => {
          if (res.errorCode === "000000") {
            this.$store.commit("user/SET_USER_INFO", res.data);
            // 系统中用的token
            this.$store.commit("user/SET_TOKEN", res.data.token);
            // 通知请求层 token 已就绪
            markTokenReady();
          }
        });
      } catch (error) {
        console.log(error);
      }
    },
  },
};
</script>

<style>
#app {
  font-family: "PingFang SC", "Helvetica Neue", Arial, sans-serif;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
  color: #2c3e50;
  background-color: #f5f5f5;
  min-height: 100vh;
}
</style>
